use crate::utils::get_app;
use anyhow::Result;
use tauri::{Manager, WebviewUrl, WebviewWindow, WebviewWindowBuilder};

pub struct WindowParams {
    pub id: String,
    pub title: String,
    pub url: String,
    pub size: (f64, f64),
    pub position: (f64, f64),
    pub resizable: Option<bool>,
    pub transparent: Option<bool>,
    pub decorations: Option<bool>,
}

pub fn create_window(params: WindowParams) -> Result<WebviewWindow> {
    let app_handle = get_app()?;
    // 创建一个新的窗口
    let progress_window =
        WebviewWindowBuilder::new(&app_handle, params.id, WebviewUrl::App(params.url.into()))
            .title(params.title)
            .inner_size(params.size.0, params.size.1)
            .resizable(params.resizable.unwrap_or(true))
            .decorations(params.decorations.unwrap_or(true))
            .transparent(params.transparent.unwrap_or(false))
            .fullscreen(false)
            .build()?;

    // 成功创建窗口
    Ok(progress_window)
}

pub fn close_window(id: String) {
    let app_handle = get_app().expect("Failed to get app");
    let windows = app_handle.windows();
    for (label, window) in windows {
        if label.as_str().contains(id.as_str()) {
            let _ = window.destroy();
        }
    }
}
